//
//  FloatingWindow.m
//  IBC
//
//  Created by 高海营 on 2017/11/13.
//  Copyright © 2017年 inspur. All rights reserved.
//

#import "FloatingWindow.h"
#import <CoreFoundation/CoreFoundation.h>
#define xh_ScreenH [UIScreen mainScreen].bounds.size.height
#define xh_ScreenW [UIScreen mainScreen].bounds.size.width

@interface FloatingWindow()

@property (nonatomic, assign)CGPoint touchStartPosition;

@end

@implementation FloatingWindow
typedef NS_ENUM(NSInteger ,xh_FloatWindowDirection) {
    xh_FloatWindowLEFT,
    xh_FloatWindowRIGHT,
    xh_FloatWindowTOP,
    xh_FloatWindowBOTTOM
};

typedef NS_ENUM(NSInteger, xh_ScreenChangeOrientation) {
    xh_Change2Origin,
    xh_Change2Upside,
    xh_Change2Left,
    xh_Change2Right
};

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

    UITouch *touch = [touches anyObject];
    self.touchStartPosition = [touch locationInView:self];
    self.touchStartPosition = [self ConvertDir:_touchStartPosition];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *touch = [touches anyObject];
    CGPoint curPoint = [touch locationInView:self];
    curPoint = [self ConvertDir:curPoint];
    
    CGFloat deltaX = curPoint.x-self.touchStartPosition.x;
    CGFloat deltaY = curPoint.y-self.touchStartPosition.y;
    self.center = CGPointMake(self.center.x+deltaX,self.center.y+deltaY);
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self checkPosition];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self checkPosition];
}
//矫正位置
- (void)checkPosition{
    CGFloat W = xh_ScreenW;
    CGFloat H = xh_ScreenH;
    CGFloat left = self.frame.origin.x;
    NSLog(@"left = %f",left);
    CGFloat right = self.frame.origin.x + self.frame.size.width;
    CGFloat top = self.frame.origin.y;
    CGFloat bottom = top + self.frame.size.height;
    
    CGFloat centerX = self.center.x;
    CGFloat centerY = self.center.y;
    
    if (left < 0) {
        centerX = self.frame.size.width/2;
    }
    if (right > W){
        centerX = W - self.frame.size.width/2;
        
    }
    
    if (top < 0) {
        centerY = self.frame.size.width/2;
    }
    if (bottom > H) {
        centerY = H - self.frame.size.width/2;
        
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.center = CGPointMake(centerX, centerY);
    }];
}
- (void)buttonRotate {
    xh_ScreenChangeOrientation change2orien = [self screenChange];
    switch (change2orien) {
        case xh_Change2Origin:
            self.transform = _originTransform;
            break;
        case xh_Change2Left:
            self.transform = _originTransform;
            self.transform = CGAffineTransformMakeRotation(-M_PI_2);
            break;
        case xh_Change2Right:
            self.transform = _originTransform;
            self.transform = CGAffineTransformMakeRotation(M_PI_2);
            break;
        case xh_Change2Upside:
            self.transform = _originTransform;
            self.transform = CGAffineTransformMakeRotation(M_PI);
            break;
        default:
            break;
    }
}

/**
 *  convert to the origin coordinate
 *
 *  UIInterfaceOrientationPortrait           = 1
 *  UIInterfaceOrientationPortraitUpsideDown = 2
 *  UIInterfaceOrientationLandscapeRight     = 3
 *  UIInterfaceOrientationLandscapeLeft      = 4
 */
- (CGPoint)ConvertDir:(CGPoint)p {
    xh_ScreenChangeOrientation change2orien = [self screenChange];
    // covert
    switch (change2orien) {
        case xh_Change2Left:
            return [self LandscapeLeft:p];
            break;
        case xh_Change2Right:
            return [self LandscapeRight:p];
            break;
        case xh_Change2Upside:
            return [self UpsideDown:p];
            break;
        default:
            return p;
            break;
    }
}

- (xh_ScreenChangeOrientation)screenChange {
    
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    
    // 1. xh_Change2Origin(1->1 | 2->2 | 3->3 | 4->4)
    if (_initOrientation == orientation) return xh_Change2Origin;
    
    // 2. xh_Change2Upside(1->2 | 2->1 | 4->3 | 3->4)
    NSInteger isUpside = orientation + _initOrientation;
    if (isUpside == 3 || isUpside == 7) return xh_Change2Upside;
    
    // 3. xh_Change2Left(1->4 | 4->2 | 2->3 | 3->1)
    // 4. xh_Change2Right(1->3 | 3->2 | 2->4 | 4->1)
    xh_ScreenChangeOrientation change2orien = xh_Change2Origin;
    switch (_initOrientation) {
        case UIInterfaceOrientationPortrait:
            if (orientation == UIInterfaceOrientationLandscapeLeft)
                change2orien = xh_Change2Left;
            else if(orientation == UIInterfaceOrientationLandscapeRight)
                change2orien = xh_Change2Right;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            if (orientation == UIInterfaceOrientationLandscapeRight)
                change2orien = xh_Change2Left;
            else if(orientation == UIInterfaceOrientationLandscapeLeft)
                change2orien = xh_Change2Right;
            break;
        case UIInterfaceOrientationLandscapeRight:
            if (orientation == UIInterfaceOrientationPortrait)
                change2orien = xh_Change2Left;
            else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
                change2orien = xh_Change2Right;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            if (orientation == UIInterfaceOrientationPortraitUpsideDown)
                change2orien = xh_Change2Left;
            else if(orientation == UIInterfaceOrientationPortrait)
                change2orien = xh_Change2Right;
            break;
            
        default:
            break;
    }
    return change2orien;
}

- (CGPoint)UpsideDown:(CGPoint)p {
    return CGPointMake(xh_ScreenW - p.x, xh_ScreenH - p.y);
}

- (CGPoint)LandscapeLeft:(CGPoint)p {
    return CGPointMake(p.y, xh_ScreenW - p.x);
}

- (CGPoint)LandscapeRight:(CGPoint)p {
    return CGPointMake(xh_ScreenH - p.y, p.x);
}
- (void)dealloc{
    NSLog(@"%s",__func__);
}
@end
